package hiresoft.ui;

import hiresoft.network.*;
import hiresoft.users.Applicant;
import javax.swing.*;

/**
 * Creates the User Interface and
 *  functionality for Applicant accounts
 *
 * @author Kevin Carlino
 */
public class ApplicantFrame extends UserFrame {
    Network net = Network.getInstance();
    DefaultListModel model;
    int jobID[];

    public ApplicantFrame(LoginFrame context, Applicant _user){
        super(context, _user);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstPostings = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtRequirements = new javax.swing.JTextArea();
        lblJobTitle = new javax.swing.JLabel();
        lblPay = new javax.swing.JLabel();
        lblDescription = new javax.swing.JLabel();
        lblJobID = new javax.swing.JLabel();
        lblDeadline = new javax.swing.JLabel();
        lblRequirements = new javax.swing.JLabel();
        lblJob = new javax.swing.JLabel();
        lblPayRate = new javax.swing.JLabel();
        lblID = new javax.swing.JLabel();
        lblDate = new javax.swing.JLabel();
        lblReferences = new javax.swing.JLabel();
        lblRefName1 = new javax.swing.JLabel();
        txtRefName1 = new javax.swing.JTextField();
        txtRefEmail1 = new javax.swing.JTextField();
        lblRefEmail1 = new javax.swing.JLabel();
        lblRefName2 = new javax.swing.JLabel();
        txtRefName2 = new javax.swing.JTextField();
        txtRefEmail2 = new javax.swing.JTextField();
        lblRefEmail2 = new javax.swing.JLabel();
        lblRefName3 = new javax.swing.JLabel();
        txtRefName3 = new javax.swing.JTextField();
        lblRefEmail3 = new javax.swing.JLabel();
        btnApply = new javax.swing.JButton();
        txtApplicantName = new javax.swing.JTextField();
        lblApplicantName = new javax.swing.JLabel();
        lblApplicantEmail = new javax.swing.JLabel();
        txtApplicantEmail = new javax.swing.JTextField();
        jScrollPane4 = new javax.swing.JScrollPane();
        txtResume = new javax.swing.JTextArea();
        lblResume = new javax.swing.JLabel();
        txtRefEmail3 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        lstPostings.setModel(new DefaultListModel());
        lstPostings.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lstPostingsMouseClicked(evt);
            }
        });
        lstPostings.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                lstPostingsKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                lstPostingsKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(lstPostings);

        txtDescription.setColumns(20);
        txtDescription.setEditable(false);
        txtDescription.setRows(5);
        jScrollPane2.setViewportView(txtDescription);

        txtRequirements.setColumns(20);
        txtRequirements.setEditable(false);
        txtRequirements.setRows(5);
        jScrollPane3.setViewportView(txtRequirements);

        lblJobTitle.setText("Job Title:");

        lblPay.setText("Pay:");

        lblDescription.setText("Description");

        lblJobID.setText("Job ID:");

        lblDeadline.setText("Deadline:");

        lblRequirements.setText("Requirements");

        lblJob.setText("Job");

        lblPayRate.setText("X.XX/hr");

        lblID.setText("123456789");

        lblDate.setText("1/1/1");

        lblReferences.setText("References");

        lblRefName1.setText("Name");

        txtRefName1.setText("Enter Text");

        txtRefEmail1.setText("Enter Text");

        lblRefEmail1.setText("Email");

        lblRefName2.setText("Name");

        txtRefName2.setText("Enter Text");

        txtRefEmail2.setText("Enter Text");

        lblRefEmail2.setText("Email");

        lblRefName3.setText("Name");

        txtRefName3.setText("Enter Text");

        lblRefEmail3.setText("Email");

        btnApply.setText("Apply");
        btnApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyActionPerformed(evt);
            }
        });

        txtApplicantName.setText("Your Name");

        lblApplicantName.setText("Name");

        lblApplicantEmail.setText("Email");

        txtApplicantEmail.setText("Your email address");

        txtResume.setColumns(20);
        txtResume.setRows(5);
        jScrollPane4.setViewportView(txtResume);

        lblResume.setText("Resume");

        txtRefEmail3.setText("Enter Text");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblResume)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                                        .addComponent(lblDescription)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblJobTitle)
                                                .addComponent(lblPay))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblPayRate)
                                                .addComponent(lblJob)))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(txtApplicantName, javax.swing.GroupLayout.DEFAULT_SIZE, 121, Short.MAX_VALUE)
                                                .addComponent(lblApplicantName))
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(lblApplicantEmail)
                                                .addComponent(txtApplicantEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                    .addGap(18, 18, 18))))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRequirements)
                            .addComponent(lblReferences)
                            .addComponent(lblRefName1)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lblDeadline)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 198, Short.MAX_VALUE)
                                .addComponent(lblDate))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(lblJobID)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, Short.MAX_VALUE)
                                .addComponent(lblID))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(lblRefName3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE))
                                    .addComponent(txtRefName1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                    .addComponent(lblRefName2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRefName2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)
                                    .addComponent(txtRefName3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txtRefEmail3, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                    .addComponent(lblRefEmail3, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRefEmail2, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                    .addComponent(lblRefEmail2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblRefEmail1, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRefEmail1, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)))
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnApply, javax.swing.GroupLayout.DEFAULT_SIZE, 574, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblJobTitle)
                            .addComponent(lblJobID)
                            .addComponent(lblJob)
                            .addComponent(lblID))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblPay)
                            .addComponent(lblDeadline)
                            .addComponent(lblDate)
                            .addComponent(lblPayRate))
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblDescription)
                            .addComponent(lblRequirements))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblReferences)
                            .addComponent(lblApplicantEmail))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(lblRefName1)
                                .addComponent(lblRefEmail1)
                                .addComponent(txtApplicantEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtApplicantName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRefName1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRefEmail1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRefName2)
                            .addComponent(lblRefEmail2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRefName2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRefEmail2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblRefName3)
                            .addComponent(lblRefEmail3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRefName3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtRefEmail3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(224, 224, 224)
                        .addComponent(lblApplicantName, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                        .addGap(37, 37, 37)
                        .addComponent(lblResume)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnApply, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lstPostingsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lstPostingsMouseClicked
        getJob();
    }//GEN-LAST:event_lstPostingsMouseClicked

    private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyActionPerformed
        int selJobID = lstPostings.getSelectedIndex();
        
        if(selJobID > -1) {
            try {
                    net.out.Insert(jobID[selJobID]);
                    net.out.Insert(txtApplicantName.getText());
                    net.out.Insert(txtApplicantEmail.getText());
                    net.out.Insert(txtResume.getText());
                    net.out.Insert(txtRefName1.getText());
                    net.out.Insert(txtRefEmail1.getText());
                    net.out.Insert(txtRefName2.getText());
                    net.out.Insert(txtRefEmail2.getText());
                    net.out.Insert(txtRefName3.getText());
                    net.out.Insert(txtRefEmail3.getText());
                    net.out.Send(Packets.P_SUBMITAPP);

                    if (net.Ready())
                    {
                        if (net.in.getPacketId() == Packets.P_SUBMITAPP)
                        {
                            if(net.in.getDWord() != 0)
                            {
                                JOptionPane.showMessageDialog(null, "Your application has been successfully submitted.");
                            }
                            else
                                JOptionPane.showMessageDialog(null, "Failed to submit application.");
                        }
                    }
                }
                catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Error: " + e);
                }
        }
        else
            JOptionPane.showMessageDialog(null, "You do not have a job selected.");
    }//GEN-LAST:event_btnApplyActionPerformed

    private void lstPostingsKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstPostingsKeyPressed

    }//GEN-LAST:event_lstPostingsKeyPressed

    private void lstPostingsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_lstPostingsKeyReleased
        getJob();
    }//GEN-LAST:event_lstPostingsKeyReleased

    /**
     * Sets the network/connection and calls the main function.
     */
    public void connect() {
        try {
        net = Network.getInstance();
        getPosts();
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "No network connection established.");
        }
    }

    /**
     * Retrieves a list of all job postings in the database and displays them.
     */
    public void getPosts() {
        model = ((DefaultListModel)lstPostings.getModel());
        try {
            net.out.Send(Packets.P_GETALLJOBS);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_GETALLJOBS)
                {
                        if(net.in.getDWord() != 0)
                        {
                            int jobNums = net.in.getDWord();
                            jobID = new int[jobNums];
                            
                            for(int i = 0; i < jobNums; i++)
                            {
                                jobID[i] = net.in.getDWord();
                                model.add(i, net.in.getString());
                            }
                        }
                        else
                            JOptionPane.showMessageDialog(null, "Failed to retrieve job listing.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

    /**
     * Retrieves specific job information and displays it.
     */
    public void getJob() {
       int selJobID = lstPostings.getSelectedIndex();

        try {
            net.out.Insert(jobID[selJobID]);
            net.out.Send(Packets.P_GETJOB);
            if (net.Ready())
            {
                if (net.in.getPacketId() == Packets.P_GETJOB)
                {
                    if(net.in.getDWord() != 0)
                    {
                        String jobTitle = net.in.getString();
                        String payRate = net.in.getString();
                        String Deadline = net.in.getString();
                        String Description = net.in.getString();
                        String Requirements = net.in.getString();

                        lblJob.setText(jobTitle);
                        lblPayRate.setText(payRate);
                        lblDate.setText(Deadline);
                        txtDescription.setText(Description);
                        txtRequirements.setText(Requirements);
                        lblID.setText(String.valueOf(jobID[selJobID]));
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Failed to retrieve job information.");
                }
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Error: " + e);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JLabel lblApplicantEmail;
    private javax.swing.JLabel lblApplicantName;
    private javax.swing.JLabel lblDate;
    private javax.swing.JLabel lblDeadline;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblID;
    private javax.swing.JLabel lblJob;
    private javax.swing.JLabel lblJobID;
    private javax.swing.JLabel lblJobTitle;
    private javax.swing.JLabel lblPay;
    private javax.swing.JLabel lblPayRate;
    private javax.swing.JLabel lblRefEmail1;
    private javax.swing.JLabel lblRefEmail2;
    private javax.swing.JLabel lblRefEmail3;
    private javax.swing.JLabel lblRefName1;
    private javax.swing.JLabel lblRefName2;
    private javax.swing.JLabel lblRefName3;
    private javax.swing.JLabel lblReferences;
    private javax.swing.JLabel lblRequirements;
    private javax.swing.JLabel lblResume;
    private javax.swing.JList lstPostings;
    private javax.swing.JTextField txtApplicantEmail;
    private javax.swing.JTextField txtApplicantName;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextField txtRefEmail1;
    private javax.swing.JTextField txtRefEmail2;
    private javax.swing.JTextField txtRefEmail3;
    private javax.swing.JTextField txtRefName1;
    private javax.swing.JTextField txtRefName2;
    private javax.swing.JTextField txtRefName3;
    private javax.swing.JTextArea txtRequirements;
    private javax.swing.JTextArea txtResume;
    // End of variables declaration//GEN-END:variables

}
